﻿using System.Data.SQLite;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Cashier
{
    /// <summary>
    /// Test.xaml 的交互逻辑
    /// </summary>
    public partial class Test : Page
    {
        private DataTable data;
        public Test()
        {
            InitializeComponent();
            ShowOrders();
        }
        private void ShowOrders()
        {
            try
            {
                //获取表格
                data = new DataTable("desktable");
                data.Columns.Add(new DataColumn("table_id", typeof(string)));
                data.Columns.Add(new DataColumn("Number_of_people", typeof(string)));


                using (SQLiteConnection connection = new SQLiteConnection(CommonValue.sqliteConnectString))
                {
                    connection.Open();
                    string cmd = "select * from desktable where Number_of_people=0";
                    using (SQLiteCommand SQLiteCommand = new SQLiteCommand(cmd, connection))
                    {
                        SQLiteDataAdapter SQLiteDataAdapter = new SQLiteDataAdapter(cmd, connection);
                        using (SQLiteDataReader reader = SQLiteCommand.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                string table_id = reader["table_id"].ToString();
                                string Number_of_people = reader["Number_of_people"].ToString();
                                data.Rows.Add(table_id, Number_of_people);
                            }

                            listView.DataContext = data.DefaultView;

                            connection.Close();
                        }
                    }
                }

            }
            catch (Exception e)
            {
                MessageBox.Show("台桌查询失败：" + e.Message);
            }
        }

        private void btn_Click(object sender, RoutedEventArgs e)
        {
            int tableid;
            int number;
            tableid = int.Parse(TableidBox.Text);
            if (numbereBox.Text.ToString() != "")
            {
                number = int.Parse(numbereBox.Text);
                try
                {
                    using (SQLiteConnection connection = new SQLiteConnection(CommonValue.sqliteConnectString))
                    {
                        connection.Open();
                        string cmd = "update desktable set Number_of_people=" + number + " where table_id=" + tableid;
                        using (SQLiteCommand SQLiteCommand = new SQLiteCommand(cmd, connection))
                        {
                            if (SQLiteCommand.ExecuteNonQuery() > 0)
                            {
                                MessageBox.Show("开台成功！");
                                ShowOrders();
                                connection.Close();
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("开台失败，请重新尝试！" + ex.Message);
                }
            }
            else
            {
                MessageBox.Show("请输入顾客数量");
            }
        }

        private void Dataview_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void listView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int index = listView.SelectedIndex;
            if (listView.SelectedIndex < 0 || listView.SelectedIndex >= listView.Items.Count) return;
            if (index >= 0 && index < data.Rows.Count)
            {
                TableidBox.Text=data.Rows[listView.SelectedIndex].ItemArray[0].ToString();
            }
        }
    }
}
